#include "stdio.h"

#include "../include/picasso.h"
#include "drawFunc.h"

static ps_rect gr = {200, 100, 320, 240};
static ps_rect gt = {100, 100, 200, 200};
static ps_point gp = {0 , 0};
static ps_matrix* pm;
static ps_path * pa;
static ps_image * pi;
static ps_pattern * pt;
static double gamma = 1.0;
static step = 0;
static ps_bool anti = False;
void draw_test (int id, ps_context* gc)
{
	ps_color col = {0, 0, 1, 1};
	ps_color sol = {1, 0, 0, 1};
    ps_rect cr = {2.7 , 3.4, 272.4, 261.3};
	ps_point s = {50.1, 50.3};
	ps_point e = {200.7, 50.3};

	ps_point p[3] = {{100,100},{200, 300},{ 300, 40}};
	ps_point cp = {128, 128};
	ps_point sc = {10, 228};
	ps_rect br = {50, 50, 120, 100};

	ps_set_line_width(gc, 1);
	ps_set_stroke_color(gc, &sol);
	ps_set_source_color(gc, &col);
	ps_arc (gc, &cp, 10 , 0, 6.28, True);
	ps_stroke(gc);

	ps_tangent_arc(gc, &br, 0, 1.254);
	ps_stroke(gc);

	ps_move_to(gc, &sc);
	ps_bezier_curve_to(gc, &p[0], &p[1], &p[2]);
	ps_arc (gc, &cp, 100 , 0.785, 3.140, False);
	ps_bezier_curve_to(gc, &p[0], &p[1], &p[2]);
	ps_quad_curve_to(gc, &p[1], &p[2]);
	ps_stroke(gc);

	ps_rectangle(gc, &cr);
	ps_rounded_rect(gc, &gt, 45.2, 35.2, 25.2, 25.2, 35.2, 55.2, 65.2, 85.2);
	ps_ellipse(gc, &gr);
	ps_stroke(gc);

	ps_move_to(gc, &s);
	ps_line_to(gc, &e);
	ps_stroke(gc);
}

void init_context (ps_context* gc, ps_canvas* cs)
{
	float version = (float)ps_version() / 10000;
	fprintf(stderr, "picasso version %.2f\n", version);

    pa = ps_path_create();
    pm = ps_matrix_create();

    ps_translate(gc, 203, 35);
}

void dini_context (ps_context* gc)
{
	ps_image_unref(pi);
    ps_matrix_unref(pm);
	ps_pattern_unref(pt);
    ps_path_unref(pa);
}

void set_image_data(unsigned char* data, ps_color_format fmt, int w, int h, int p)
{
	pi = ps_image_create_with_data(data, fmt, w, h, p);
}

void set_pattern_data(unsigned char* data, ps_color_format fmt, int w, int h, int p)
{
	ps_image* pam = ps_image_create_with_data(data, fmt, w, h, p);
	pt = ps_pattern_create_image(pam, WRAP_TYPE_REPEAT, WRAP_TYPE_REPEAT, pm);
}

void timer_action(ps_context* gc)
{
	step ++;
	if (step > 5) {
		step = 0;
		ps_set_gamma(gc, gamma);
		gamma++;
	}
	if (gamma > 5) {
		gamma = 0.05;
		ps_set_antialias(gc, anti);
		if (!anti) anti = True;
		else anti = False;
	}
}

